package com.locatme.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.locatme.adapter.FriendViewAdapter;
import com.locatme.model.Utilisateur;
import com.locatme.modelBD.ListeAmisBD;
import com.locatme.modelBD.UtilisateurBD;
import com.main.locatme.R;

public class FriendsFragment extends Fragment {

    private ListeAmisBD addAmis;
    private EditText txtami;
    private Utilisateur user;
    private FriendViewAdapter adapter;
    private List<String> listAmis;
    private ListeAmisBD amisBD;
    private ListView listView;
    private boolean instanceview;

    public FriendsFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_friends, container,
                false);
        user = (Utilisateur) getActivity().getIntent().getExtras().getParcelable("user");
        txtami = (EditText) rootView.findViewById(R.id.txtfriend);
        Button btnAjout = (Button) rootView.findViewById(R.id.btnaddfriend);
        setAmisBD(new ListeAmisBD(getActivity()));
        getAmisBD().open();

        //Récupération de l'objet définissant l'utilisateur connect
        user = (Utilisateur) getActivity().getIntent().getExtras().getParcelable("user");

        listAmis = new ArrayList<String>();
        listAmis = getAmisBD().getFriendByLogin(user.getLogin());
        if(listAmis!=null){
            instanceview=true;
            adapter = new FriendViewAdapter(getActivity(),R.id.listviewfriend, listAmis);
            setListView((ListView) rootView.findViewById(R.id.listviewfriend));
            getListView().setAdapter(adapter);
        } else{
            instanceview= false;
        }
        btnAjout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ajouterAmi(v);
                if(instanceview){
                    listAmis = getAmisBD().getFriendByLogin(user.getLogin());
                    adapter = new FriendViewAdapter(getActivity(),R.id.listviewfriend, listAmis);
                    adapter.setEntries(listAmis);
                    getListView().setAdapter(adapter);
                } else {
                    Toast.makeText(getActivity(), "Refresh to see your Friend list",
                            Toast.LENGTH_LONG).show();
                }
            }

        });

        return rootView;
    }

    public void ajouterAmi(View v) {
        addAmis = new ListeAmisBD(getActivity());
        addAmis.open();
        UtilisateurBD userBD = new UtilisateurBD(getActivity());
        userBD.open();
        if (userBD.getPersonneByLogin(txtami.getText().toString()) != null) {
            addAmis.AddFriend(user.getLogin(), txtami.getText().toString());
            addAmis.close();
            txtami.setText("");
            Toast.makeText(getActivity(), "New friend added",
                    Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getActivity(), "This user does not exist",
                    Toast.LENGTH_LONG).show();
        }
    }

    public ListView getListView() {
        return listView;
    }

    public void setListView(ListView listView) {
        this.listView = listView;
    }

    public ListeAmisBD getAmisBD() {
        return amisBD;
    }

    public void setAmisBD(ListeAmisBD amisBD) {
        this.amisBD = amisBD;
    }
}
